package co.za.alacrity.gui;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.SpinnerModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSpinner;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import java.awt.Component;
import javax.swing.SwingConstants;

import co.za.alacrity.obseverpattern.ButtonListener;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class buttonPanel extends JPanel{
	
	private JSpinner spin;
	
	
	public buttonPanel(final ButtonListener buttonListener) {
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(0, 1, 5, 5));
		
		JButton btnSolution = new JButton("Generate Solution");
		buttonPanel.setSize(10, 5);
		buttonPanel.add(btnSolution);
		
		
		JButton btnRestore = new JButton("Restore");
		btnRestore.setSize(10, 15);
		buttonPanel.add(btnRestore);
		
		JButton btnNew = new JButton("New Game");
		btnNew.setSize(10, 15);
		buttonPanel.add(btnNew);
		
		JButton btnExit = new JButton("Exit");
		btnExit.setSize(10, 15);
		buttonPanel.add(btnExit);
		add(buttonPanel);
		
		final SpinnerNumberModel modal = new SpinnerNumberModel(0, 0, 9, 1);
		spin = new JSpinner(modal );
		buttonPanel.add(spin);
		spin.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				buttonListener.valuedChanged(modal.getNumber().intValue());
				
			}
		});
		
		btnSolution.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				buttonListener.generate();
				
			}
		});
		btnRestore.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonListener.restore();
				
			}
		});
		btnExit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonListener.exit();
			}
		});
		btnNew.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				buttonListener.newGame();
			}
		});
	}

	public void setValue(int sudokuValue) {
		spin.setValue(sudokuValue);
	}
	
	
	
	

}
